prometheus + grafana基本配置
标签:prometheus grafana
环境介绍
环境准备
1 | yum install ntpdate -y |
prometheus安装
- prometheus官网 https://prometheus.io/download/
下载prometheus
1 | wget https://github.com/prometheus/prometheus/releases/download/v2.4.3/prometheus-2.4.3.linux-amd64.tar.gz |
解压运行prometheus
1 | tar zxvf prometheus-2.4.3.linux-amd64.tar.gz |
创建启动方式
1 | vim /usr/lib/systemd/system/prometheus.service |
node_exporter安装
exporter监控插件有很多比如mysql_exporter、memcached_exporter、statsd_exporter、blackbox_exporter等这里我们选择基本的node_exporter
下载node_exporter
1 | wget https://github.com/prometheus/node_exporter/releases/download/v0.16.0/node_exporter-0.16.0.linux-amd64.tar.gz |
解压运行node_exporter
1 | tar zxvf node_exporter-0.16.0.linux-amd64.tar.gz |
创建启动方式
1 | vim /usr/lib/systemd/system/node.service |
加入prometheus监控
1 | - job_name: 'node_exporter' |
监控示例
1 | 示例一 |
pushgateway安装
下载pushgateway
1 | wget https://github.com/prometheus/pushgateway/releases/download/v0.6.0/pushgateway-0.6.0.linux-amd64.tar.gz |
解压运行pushgateway
1 | tar zxvf pushgateway-0.6.0.linux-amd64.tar.gz |
创建启动方式
1 | vim /usr/lib/systemd/system/pushgateway.service |
加入prometheus监控
1 | - job_name: 'pushgateway' |
grafana安装
下载grafana
1 | wget https://s3-us-west-2.amazonaws.com/grafana-releases/release/grafana-5.3.2-1.x86_64.rpm |
安装运行grafana
1 | yum localinstall grafana-5.3.2-1.x86_64.rpm |
自定业务监控脚本
实现原理
使用prometheus隔一段时间会从pushgateway提取数据我们只要将数据实时推送到pushgateway上就可以将数据纳入监控脚本
监控示例
- 提取出nginx一个小时内404错误的数量
1 |
|
- 网站访问时间
1 |
|
- 检测内网丢包
1 |
|
- 内网延迟率
1 |
|
- 检测等待连接数
1 |
|
python编写客户端
prometheus提供了多种语言编写监控客户端监控示例
环境准备
1 | pip install prometheus_client #安装prometheus客户端模块 |
- 简单示例
1 | # !/usr/bin/python |
- 监控cpu user使用时间比
1 | #!/usr/bin/python |